package com.mch.main.common;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;

public class MDebugDLG extends JDialog {
	private static final long serialVersionUID = -2712541381113667908L;

	private MDebugPanel debugPanel = new MDebugPanel();

	private MDebugDLG() {
		super();
		initGUI();
	}

	private void initGUI() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 605, 434);
		getContentPane().setLayout(new BorderLayout());
		JPanel buttonPane = new JPanel();

		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);
		JButton okButton = new JButton("清除");

		okButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				getTextArea().setText("");
			}
		});

		okButton.setActionCommand("OK");
		buttonPane.add(okButton);
		getRootPane().setDefaultButton(okButton);
		JButton cancelButton = new JButton("Cancel");
		cancelButton.setActionCommand("Cancel");
		buttonPane.add(cancelButton);

		getContentPane().add(debugPanel, BorderLayout.CENTER);
	}

	public RSyntaxTextArea getTextArea() {
		return getDebugPanel().getTextArea();
	}

	public MDebugPanel getDebugPanel() {
		return debugPanel;
	}

	@Override
	public void setVisible(boolean b) {
		super.setVisible(b);
	}

	public static void debug(Object obj) {
		String str = null;
		if (obj instanceof String) {
			str = (String) obj;
		} else {
			str = obj.toString();
		}

		PlainDocument textdoc = (PlainDocument) dlg.getTextArea().getDocument();
		try {
			textdoc.insertString(textdoc.getLength(), "\n" + str, null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
	}

	private static MDebugDLG dlg = new MDebugDLG();

	public static MDebugDLG getInstance() {
		return dlg;
	}
}
